<?php

namespace App\Http\Controllers\Wechat;

use App\Http\Controllers\Controller;
use App\Models\yb\ActivityModel;
use Illuminate\Http\Request;
use EasyWeChat\Factory;
use Illuminate\Support\Facades\Log;
use EasyWeChat\MiniProgram\DataCube\Client;


class WechatController extends Controller
{

    /**
     * Get a JWT via given credentials.
     *  服务器验证
     * @return \Illuminate\Http\JsonResponse
     */
    public function dispose(Request $request)
    {

        $config = [
            'app_id' => 'wx9a67fcdacb9b9256',
            'secret' => '81fad759102bd90044c154fedd40d3bc',
        ];

        $app = Factory::officialAccount($config);

        $app->base->httpPostJson('wxa/generate_urllink',[
            'path' => 'pages/index/index',
            'is_expire' => true,
            'expire_type' => 1,
            'expire_interval' => 1
        ]);

        Client::httpPostJson();

        $app->server->push(function ($message) {
            Log::info(var_export($message, true));
            $event = $message['event'];
            // $message['FromUserName'] // 用户的 openid就这样 等下
            // $message['MsgType'] // 消息类型：event, text....
            return "您好！欢迎使用 EasyWeChat";
        });

        $response = $app->server->serve();

        // 将响应输出
        return $response;
    }

    /**
     * Get a JWT via given credentials.
     *  推送
     * @return \Illuminate\Http\JsonResponse
     */
    public function push(Request $request)
    {

        $config = [
            'app_id' => 'wx3cfd9698645432e9',
            'secret' => '0b5b897aa5751aa1e414ce1a15d6d6d5',
            'token' => 'C1089F8BE36698A4EB575EDBB5919B60',
            'response_type' => 'array',
            'aes_key' => '461wQLRvxu4j7nGfm0FASGHFpTY1lm2JfJyWgzqWTgP'
        ];

        $app = Factory::officialAccount($config);

        $data = $app->template_message->send([
            'touser' => 'osHrP6tHg_3MYV47hyag87iudHUU',
            'template_id' => 'WGbDbd6L19-dgwoF9VVFRcvl3VVpdMs87d5_yEY-gtQ',
            // 'url' => 'https://easywechat.org',
            'miniprogram' => [
                    'appid' => 'wx2daad6e45cc01181',
                    'page' => 'pages/index/index',
            ],
            'data' => [
                'first' => '有新订单创建，请迅速响应。',
                'keyword1' => '寄件订单',
                'keyword2' => 'J00001',
                'keyword3' => date('Y-m-d H:i:s',time()),
                'remark' => '订单全部信息请点击“详情”进行查看'
            ],
        ]);
        dd($data);

    }

}
